<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<link rel="stylesheet" type="text/css" href="<c:url value='/css/teacher-index.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/css/header.css'/>" />
<style>
	#upload-img:hover{
		cursor:pointer
	}
</style>
<script>
var CONTEXT_PATH = "<c:url value='/'/>";
</script>

<div class="row">
	<div class="col-xs-12 teachIndex-title">
		<div
			class="col-xs-12 col-sm-6 col-md-6 col-lg-6 teachIndex-title-left">
			<shiro:hasRole name="teacher">
				<a href="${pageContext.request.contextPath}/course/list">精准微课堂</a>
			</shiro:hasRole>
			<shiro:hasRole name="assistant">
				<a href="${pageContext.request.contextPath}/accontrol/ajaxListCourseView">精准微课堂</a>
			</shiro:hasRole>
			<shiro:hasRole name="student">
				<a href="${pageContext.request.contextPath}/lesson/studentCLesson">精准微课堂</a>
			</shiro:hasRole>
		</div>
		<div
			class="col-xs-12 col-sm-6 col-md-6 col-lg-6 teachIndex-title-right" style="text-align:right;">
			<img id="upload-img" title="点击更换头像" src="<c:url value="/resource/picture/${image}" />" />${name}
			<a href="<c:url value='/user/update' />" title="修改个人信息">[修改]</a>
			<a href="<c:url value='/user/logout' />" title="注销当前账号">[退出]</a>
			<input id="previewImg" type="file" class="form-control" name="file" style="display:none;" >
			<input type="hidden" class="form-control" name="img-file" >
		</div>

	</div>
</div>

<script>
	<%--console.log(${image});--%>
$("#upload-img").click(function(){
	document.getElementById("previewImg").click();
	$("#previewImg").change(function(){
	    var objUrl = getObjectURL(this.files[0]) ;
	    if (objUrl) {
	        $("#upload-img").attr("src", objUrl) ;
	        ajaxFileUpload();
	    } 
	}) ;
	function getObjectURL(file) {
	    var url = null ; 
	    if (window.createObjectURL!=undefined) { // basic
	        url = window.createObjectURL(file) ;
	    } else if (window.URL!=undefined) { // mozilla(firefox)
	        url = window.URL.createObjectURL(file) ;
	    } else if (window.webkitURL!=undefined) { // webkit or chrome
	        url = window.webkitURL.createObjectURL(file) ;
	    }
	    return url ;
	}
});

function ajaxFileUpload() {
	$.ajaxFileUpload({
		url :CONTEXT_PATH+"resource/upload", //用于文件上传的服务器端请求地址
		secureuri : false, //是否需要安全协议，一般设置为false
		fileElementId : 'previewImg', //文件上传域的ID
		dataType : 'json', //返回值类型 一般设置为json
		success : function(data, status) //服务器成功响应处理函数
		{
			if(data.status=="success"){
				uploadImg(data.data.key);
			}else{
				layer.alert(data.message);
			}
		},
		error : function(data, status, e)//服务器响应失败处理函数
		{
			layer.alert(e);
		}
	})
	return false;
};
	
var uploadImg = function(key) {
	var json = {
		picture:key
	};
	$.post(CONTEXT_PATH+"user/changeAvatar", {
		data : JSON.stringify(json)
	}, function(data) {
		if (data.status == "success") {
			location.reload();
		} else {
			layer.alert(data.message);
		}
	});
}
</script>